package com.example.yls.demoa;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnQuery;
    private TextView txtResult;
    private EditText edtStuno;
    private EditText edtName;
    private EditText edtAge;
    private Button btnAdd;
    private EditText delStuno;
    private Button btnDel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
        * 查询content_provider 中的ContentProvider
        * */
        txtResult=findViewById(R.id.txt_result);
        btnQuery=findViewById(R.id.btn_query);
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                Cursor cursor=getContentResolver().query(uri,null,null,
                        null,null);
                if(cursor!=null && cursor.getCount()>0){
                    StringBuffer buffer=new StringBuffer();
                    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        String stuno=cursor.getString(0);
                        String name=cursor.getString(1);
                        int age=cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(name).append("   ").append(age).
                                append("\n");
                    }
                    txtResult.setText(buffer);
                }else{
                    txtResult.setText("行数为0");
                }
            }
        });
        /*
        * 实现添加学生
        * */
        edtStuno=findViewById(R.id.edt_stuno);
        edtName=findViewById(R.id.edt_name);
        edtAge=findViewById(R.id.edt_age);
        btnAdd=findViewById(R.id.btn_add);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String no=edtStuno.getText().toString();
                String name=edtName.getText().toString();
                int age=Integer.parseInt(edtAge.getText().toString());
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                ContentValues contentValues=new ContentValues();
                contentValues.put("stuno",no);
                contentValues.put("name",name);
                contentValues.put("age",age);
                // getContentResolver:内容解析   StuContentProvider
                getContentResolver().insert(uri,contentValues);
            }
        });
        /*
        * 实现删除学生 ?
        * *//*
        delStuno=findViewById(R.id.del_stuno);
        btnDel=findViewById(R.id.btn_del);

        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String no=delStuno.getText().toString();
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                String where="stuno="+no;
                int rows=getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"删除了"+rows+"行",
                        Toast.LENGTH_SHORT).show();
            }
        });*/
    }
}
